import Vue from 'vue'
import VueRouter from 'vue-router'
const login = ()=>import ('../views/login.vue')
const layout = ()=>import ('../components/layout.vue')
const menu = ()=>import ('../views/menu/index.vue')
const role = ()=>import ('../views/role/index.vue')
const admin = ()=>import ('../views/admin/index.vue')
const category = ()=>import ('../views/category/index.vue')
const specs = ()=>import ('../views/specs/index.vue')
const goods = ()=>import ('../views/goods/index.vue')
const index = ()=>import ('../views/index.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: layout,
    children: [
      {
        path: '', // 默认的子路由
        component: index,
        meta: {
          title: '首页'
        }
      },
      {
        path: 'menu',
        component: menu,
        meta: {
          title: '菜单管理'
        }
      },
      {
        path: 'role',
        component: role,
        meta: {
          title: '角色管理'
        }
      },
      {
        path: 'admin',
        component: admin,
        meta: {
          title: '管理员管理'
        }
      },
      {
        path: 'category',
        component: category,
        meta: {
          title: '商品分类'
        }
      },
      {
        path: 'specs',
        component: specs,
        meta: {
          title: '商品规格'
        }
      },
      {
        path: 'goods',
        component: goods,
        meta: {
          title: '商品管理'
        }
      }
    ]
  },
  {
    path: '/login',
    component: login,
    meta: {
      title: '欢迎登录'
    }
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  // base: 'offcn', // 固定的前缀
  routes
})
// 全局前置导航守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || 'u-admin';
  // login页面无需登录，其他都要登录
  if(to.path === '/login') {
    next();
  }else {
    // 判断是否登录
    let userInfo = JSON.parse(localStorage.getItem('userInfo')) || {};
    if(userInfo.token) {
      // 如果登录了，就正常跳转
      next();
    }else {
      // 没有登录就回到登录页
      next('/login');
    }
  }
})

export default router
